package org.qiancizhan.UI;

import org.qiancizhan.Controller.VocabularyListController;
import org.qiancizhan.Model.POJO.VocabularyListPOJO;
import org.qiancizhan.Utils.UIFormatUtli;

import java.util.List;
import java.util.Scanner;

public class VocabularyUI {
    private static final VocabularyListController vocabularyListController = new VocabularyListController();
    private static final Scanner sc = new Scanner(System.in);
    private static final BookWordsUI bookWordsUI = new BookWordsUI();

    public static void showIndexMenu() {
        boolean validSelection = false;
        while (!validSelection) {
            UIFormatUtli.log("词书管理页\t1.查看词书\t2.添加词书\t3.删除词书\t0.返回");
            switch (sc.nextInt()) {
                case 0 -> validSelection = true;
                case 1 -> {
                    List<VocabularyListPOJO> vocabularyUserListData = vocabularyListController.getUserVocabularyList();
                    if (vocabularyUserListData.size() == 0) {
                        System.out.println("您还没有自己的词书，先去添加一下词书吧!");
                        break;
                    }
                    showUserVocabularyListMenu(vocabularyUserListData);
                }
                case 2 -> {
                    showAddVocabularyMenu();
                }
                case 3 -> {
                    showDeleteVocabularyMenu();
                }
                default -> {
                    System.out.println("请选择正确的选项!");
                }
            }
        }
    }

    private static void showUserVocabularyListMenu(List<VocabularyListPOJO> vocabularyUserListData) {
        while (true) {
            for (int i = 0; i < vocabularyUserListData.size(); i++)
                System.out.println(String.format("选项%d:\t词书：%s\t单词数量：%d\t描述：%s", (i + 1), vocabularyUserListData.get(i).getBook_name(), vocabularyUserListData.get(i).getWord_count(), vocabularyUserListData.get(i).getDescription()));
            UIFormatUtli.log("请选择要查看的词书\t0.返回");
            int sel = sc.nextInt();
            if (sel == 0) break;
            else if (sel > vocabularyUserListData.size() || sel < 1) System.out.println("请选择正确的选项!");
            else
                bookWordsUI.showBookIndex(vocabularyUserListData.get(sel - 1));
        }
    }

    private static void showDeleteVocabularyMenu() {
        List<VocabularyListPOJO> vocabularyUserListData = vocabularyListController.getUserVocabularyList();
        if (vocabularyUserListData.size() == 0) {
            System.out.println("您还没有自己的词书，先去添加一下词书吧!");
            return;
        }
        ;
        UIFormatUtli.log("请选择要删除的词书\t");
        for (int i = 0; i < vocabularyUserListData.size(); i++)
            System.out.println(String.format("选项%d:\t词书：%s\t单词数量：%d\t描述：%s", (i + 1), vocabularyUserListData.get(i).getBook_name(), vocabularyUserListData.get(i).getWord_count(), vocabularyUserListData.get(i).getDescription()));
        while (true) {
            UIFormatUtli.log(String.format("请根据选项删除词书\t0.返回词书管理页"));
            int sel = sc.nextInt();
            if (sel == 0) break;
            else if (sel > vocabularyUserListData.size() || sel < 1) System.out.println("请选择正确的选项!");
            else {
                if (vocabularyListController.deleteUserVocabularyList(vocabularyUserListData.get(sel - 1).getId()))
                    System.out.println(String.format("删除成功！删除的词书是：%s", vocabularyUserListData.get(sel - 1).getBook_name()));
                else System.out.println("该词书不存在您的列表中！");
            }
        }
    }

    private static void showAddVocabularyMenu() {
        UIFormatUtli.log("请选择要添加的词书\t");
        List<VocabularyListPOJO> vocabularyListData = vocabularyListController.getVocabularyList();
        for (int i = 0; i < vocabularyListData.size(); i++)
            System.out.println(String.format("选项%d:\t词书：%s\t单词数量：%d\t描述：%s", (i + 1), vocabularyListData.get(i).getBook_name(), vocabularyListData.get(i).getWord_count(), vocabularyListData.get(i).getDescription()));
        while (true) {
            UIFormatUtli.log(String.format("请根据选项添加词书\t0.返回词书管理页"));
            int sel = sc.nextInt();
            if (sel == 0) break;
            else if (sel > vocabularyListData.size() || sel < 1) System.out.println("请选择正确的选项!");
            else {
                if (vocabularyListController.addUserVocabularyList(vocabularyListData.get(sel - 1).getId()))
                    System.out.println(String.format("添加成功！添加的词书是：%s", vocabularyListData.get(sel - 1).getBook_name()));
                else System.out.println("您已经添加该词书，无法添加！");
            }
        }
    }


}
